package com.bbs.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.bbs.bean.BbsReplyInfo;
import com.bbs.bean.BbsTitleInfo;
import com.bbs.bean.BbsUserInfo;
import com.bbs.services.BbsReplyInfoManager;
import com.bbs.services.BbsTitleInfoManager;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class BbsReplyInfoAction extends ActionSupport implements Preparable,ModelDriven<BbsReplyInfo> {

	private static final String  LIST="/bbsReplyInfo/bbsReplyInfoList.jsp";
	private static final String  EDIT="/bbsReplyInfo/bbsReplyInfoEdit.jsp";
	private static final String  ADD="/bbsReplyInfo/bbsReplyInfoAdd.jsp";
	private static final String  SHOW="/bbsReplyInfo/bbsReplyInfoShow.jsp";
	private static final String  REPLYSHOW="/bbsTitleInfo/bbsTitleReplyShow.jsp";
	
	private BbsReplyInfo bbsReplyInfo;
	private BbsTitleInfo bbsTitleInfo;
	private BbsReplyInfoManager bbsReplyInfoManager;
	private BbsTitleInfoManager bbsTitleInfoManager;
	

	private int id;
	private List<BbsReplyInfo> bbsReplyInfoList;
	private int page;
	private int maxPage;
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public BbsReplyInfo getBbsReplyInfo() {
		return bbsReplyInfo;
	}

	public void setBbsReplyInfo(BbsReplyInfo bbsReplyInfo) {
		this.bbsReplyInfo = bbsReplyInfo;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<BbsReplyInfo> getBbsReplyInfoList() {
		return bbsReplyInfoList;
	}

	public void setBbsReplyInfoList(List<BbsReplyInfo> bbsReplyInfoList) {
		this.bbsReplyInfoList = bbsReplyInfoList;
	}

	public void setBbsReplyInfoManager(BbsReplyInfoManager bbsReplyInfoManager) {
		this.bbsReplyInfoManager = bbsReplyInfoManager;
	}
	public BbsTitleInfo getBbsTitleInfo() {
		return bbsTitleInfo;
	}

	public void setBbsTitleInfo(BbsTitleInfo bbsTitleInfo) {
		this.bbsTitleInfo = bbsTitleInfo;
	}

	public void setBbsTitleInfoManager(BbsTitleInfoManager bbsTitleInfoManager) {
		this.bbsTitleInfoManager = bbsTitleInfoManager;
	}
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		if(bbsReplyInfo==null){
			bbsReplyInfo =new BbsReplyInfo();
		}
	}

	public BbsReplyInfo getModel() {
		// TODO Auto-generated method stub
		return bbsReplyInfo;
	}
	public String list(){
		bbsReplyInfoList=bbsReplyInfoManager.findAll();
		return LIST;
	}
	
	public String addShow(){

		return ADD;
	}
	
	public String edit(){
		bbsReplyInfo=bbsReplyInfoManager.findId(id);
		return EDIT;
	}
	
	public String updateInfo(){
		bbsReplyInfoManager.update(bbsReplyInfo);
		bbsReplyInfoList=bbsReplyInfoManager.findAll();
		
		return LIST;
	}
	
	public String addInfo(){
		BbsUserInfo user=(BbsUserInfo)ServletActionContext.getRequest().getSession().getAttribute("bbsUserInfo");
		bbsReplyInfo.setReplyUserid(user.getUserid());
		bbsReplyInfo.setReplyUsername(user.getUsername());
		bbsReplyInfoManager.save(bbsReplyInfo);
		bbsReplyInfoList=bbsReplyInfoManager.findAll();
		return LIST;
	}
	
	public String show(){
		bbsReplyInfo=bbsReplyInfoManager.findId(id);
		return SHOW;
	}
	
	public String showReply(){
		if(page==0){
			page=1;
		}
		bbsReplyInfoList=bbsReplyInfoManager.getReplyByTitleId(id,page);
		bbsTitleInfo=bbsTitleInfoManager.findId(id);
		return REPLYSHOW;
	}
}
